package com.shawn.baseappbygroovydsl

import android.os.Bundle
import android.view.Gravity
import android.view.Menu
import android.view.View
import android.view.animation.OvershootInterpolator
import android.widget.TextView
import android.widget.Toast
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.navigation.NavigationView
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.drawerlayout.widget.DrawerLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.shawn.baseappbygroovydsl.base.AppBase
import com.shawn.baseappbygroovydsl.databinding.ActivityMainBinding
import com.shawn.baseappbygroovydsl.ui.extension.ExtensionLog.log
import com.shawn.baseappbygroovydsl.utils.UtilTime

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setSupportActionBar(binding.appBarMain.toolbar)

        binding.appBarMain.fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null)
                .setAnchorView(R.id.fab).show()
        }
        val drawerLayout: DrawerLayout = binding.drawerLayout
        val navView: NavigationView = binding.navView
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_sp
            ), drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
        val oneDay = 1000 * 60 * 60 * 24
        val oneMinutes = 1000 * 60 * 60
        val a = 1749717863000L
        val b = a + oneDay
        val diff = UtilTime.calculateDayDifferenceV2(a,b)
        "diff = $diff a = $a b = $b".log()
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }

    fun showFullScreenFrag(frag: Fragment) {
        supportFragmentManager.beginTransaction()
            .add(R.id.fl_frag_container, frag, frag::class.java.name)
            .commitAllowingStateLoss()
    }

    fun showFullScreenFrag(frag: Fragment, enterAnimation: Int = -1) {
        val transaction = supportFragmentManager.beginTransaction()
        if (enterAnimation != -1) {
            transaction.setCustomAnimations(enterAnimation, 0)
        }
        transaction.add(R.id.fl_frag_container, frag, frag::class.java.name)
            .commitAllowingStateLoss()
    }

    fun removeFullScreenFrag(tag: String) {
        supportFragmentManager.findFragmentByTag(tag)?.let {
            supportFragmentManager.beginTransaction()
                .remove(it)
                .commitAllowingStateLoss()
        }
    }

    fun showAppToast(str: String) {
        val toast = Toast(this)
        val toastView = View.inflate(this, R.layout.layout_toast, null)
        val tv = toastView.findViewById<TextView>(R.id.tv_app_toast_v2)
        tv.text = str
        toast.view = toastView
        toast.setGravity(Gravity.CENTER, 0, 0)
        toast.duration = Toast.LENGTH_SHORT
        toast.show()
        toastView.animate().scaleX(1f)
            .scaleY(1f)
            .setInterpolator(OvershootInterpolator())
            .setDuration(350L).start()
    }

    override fun onBackPressed() {
        if (AppBase.instance.dispatcherOnKeyCodeBack()) {
            return
        }
        super.onBackPressed()
    }
}